﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:15:59
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{

   
    public class  ArticleRepository : Repository<Article, Article, TbArticle>, IArticleRepository<Article>
    {
        public override Article CreateEntity()
        {
            return new Article { };
        }
        public override IQueryable<Article> Get()
        {
            return MapQueryable(Entities.TbArticle);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbArticle).Count();
        }
        public override IQueryable<Article> Get(Expression<Func<Article, bool>> expression)
        {
            return MapQueryable(Entities.TbArticle).Where(expression);
        }
        public override IQueryable<Article> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbArticle), sort, order);
        }
        public override IQueryable<Article> GetSort(Expression<Func<Article, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbArticle).Where(expression), sort, order);
        }
        public override void Add(Article t)
        {
            Entities.AddToTbArticle(ReverseMapModel(t));
        }
        public override void Update(Article t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(Article t)
        {
            if (Entities.TbArticle.Any(o => o.CoArticleId == t.Id))
            {

            }
            else
            {
                Entities.AddToTbArticle(ReverseMapModel(t));
            }
        }
        public override void Delete(Article t)
        {
            Entities.TbArticle.Where(o => o.CoArticleId == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<Article, bool>> expression)
        {
            MapQueryable(Entities.TbArticle).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }
       
        public JqEasyuiDataGrid<Article> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<Article>();
        }
      
        public override sealed IQueryable<Article> MapQueryable(IQueryable<TbArticle> queryable)
        {
            return queryable.Select(o => new Article 
            {
         
	           Id=o.CoArticleId,
	           Name = o.CoName 
,
	           CreateTime = o.CoCreateTime??DateTime.Now 
,
	           IsDelete = o.CoIsDelete??false 
,
	           Version = o.CoVersion??0 
,
	           IsClosed = o.CoIsClosed??false 
,
	           Culture = o.CoCulture 
,
	           Code = o.CoCode 
,
	           CategoryId = o.CoCategoryId 
,
	           MainTitle = o.CoMainTitle 
,
	           SubTitle = o.CoSubTitle 
,
	           Author = o.CoAuthor 
,
	           Content = o.CoContent 
,
	           OriginalLink = o.CoOriginalLink 
,
	           VisitCount = o.CoVisitCount??0 
,
	           IsRecommend = o.CoIsRecommend??false 
,
	           Sort = o.CoSort??0 
,
	           NormalImage = o.CoNormalImage 
,
	           SmallImage = o.CoSmallImage 
            
            });
        }
      
        public override sealed IQueryable<TbArticle> ReverseMapQueryable(IQueryable<Article> queryable)
        {
            return queryable.Select(o => new TbArticle 
            { 
        
	           CoArticleId=o.Id,
	           CoName = o.Name
,
	           CoCreateTime = o.CreateTime
,
	           CoIsDelete = o.IsDelete
,
	           CoVersion = o.Version
,
	           CoIsClosed = o.IsClosed
,
	           CoCulture = o.Culture
,
	           CoCode = o.Code
,
	           CoCategoryId = o.CategoryId
,
	           CoMainTitle = o.MainTitle
,
	           CoSubTitle = o.SubTitle
,
	           CoAuthor = o.Author
,
	           CoContent = o.Content
,
	           CoOriginalLink = o.OriginalLink
,
	           CoVisitCount = o.VisitCount
,
	           CoIsRecommend = o.IsRecommend
,
	           CoSort = o.Sort
,
	           CoNormalImage = o.NormalImage
,
	           CoSmallImage = o.SmallImage
  
            
            });
        }
      
        public override sealed Article MapModel(TbArticle model)
        {
            return new Article {
	           Id=model.CoArticleId,
	           Name = model.CoName
,
	           CreateTime = model.CoCreateTime??DateTime.Now
,
	           IsDelete = model.CoIsDelete??false
,
	           Version = model.CoVersion??0
,
	           IsClosed = model.CoIsClosed??false
,
	           Culture = model.CoCulture
,
	           Code = model.CoCode
,
	           CategoryId = model.CoCategoryId
,
	           MainTitle = model.CoMainTitle
,
	           SubTitle = model.CoSubTitle
,
	           Author = model.CoAuthor
,
	           Content = model.CoContent
,
	           OriginalLink = model.CoOriginalLink
,
	           VisitCount = model.CoVisitCount??0
,
	           IsRecommend = model.CoIsRecommend??false
,
	           Sort = model.CoSort??0
,
	           NormalImage = model.CoNormalImage
,
	           SmallImage = model.CoSmallImage
            };
        }
        
        public override sealed TbArticle ReverseMapModel(Article model)
        {
            return new TbArticle { 
	           CoArticleId=model.Id,
	           CoName = model.Name
	        
,
	           CoCreateTime = model.CreateTime
	        
,
	           CoIsDelete = model.IsDelete
	        
,
	           CoVersion = model.Version
	        
,
	           CoIsClosed = model.IsClosed
	        
,
	           CoCulture = model.Culture
	        
,
	           CoCode = model.Code
	        
,
	           CoCategoryId = model.CategoryId
	        
,
	           CoMainTitle = model.MainTitle
	        
,
	           CoSubTitle = model.SubTitle
	        
,
	           CoAuthor = model.Author
	        
,
	           CoContent = model.Content
	        
,
	           CoOriginalLink = model.OriginalLink
	        
,
	           CoVisitCount = model.VisitCount
	        
,
	           CoIsRecommend = model.IsRecommend
	        
,
	           CoSort = model.Sort
	        
,
	           CoNormalImage = model.NormalImage
	        
,
	           CoSmallImage = model.SmallImage
	        
            
            
            };
        }


    }
}



